import waterMeshTool from '../lib/waterMeshTool'
export default {
    data() {
        return {
        }
    },
    computed: {
        basin() {
            return this.$store.getters.basin
        },
        pageState() {
            return this.$store.getters.pageState
        }
    },
    watch: {
        $route: {
            handler: function (val, oldVal) {
                if (val.path !== '/Rehearsal' || val.path !== '/reserveplan/previewPlan') {
                    this.clearYYYMFW()
                }
            },
            // 深度观察监听
            deep: true
        },
        pageState: {
            immediate: true,
            deep: true,
            handler: function (val, oldVal) {
                if (this.$route.path === '/Rehearsal') {
                    if (!val.yuYan_Load && val.yuYanInfoType === 'FXPG') {
                        this.getMeshYYYMFW(val.planId)
                    } else {
                        this.clearYYYMFW()
                    }
                }
                if (this.$route.path === '/reserveplan/previewPlan') {
                    if (!val.yuAn_YYYA_Load && val.yuAn_YYYA_Right_Type === 1) {
                        this.getMeshYYYMFW(val.planId)
                    } else {
                        this.clearYYYMFW()
                    }
                }
            }
        }
    },
    beforeDestroy() {
        this.clearYYYMFW()
    },
    created() {
        var colors05 = waterMeshTool.gradient('#00A2FB', '#F0C90B', 20)
        var colors51 = waterMeshTool.gradient('#F0C90B', '#F0810B', 20)
        var colors12 = waterMeshTool.gradient('#F0810B', '#CE1F1F', 20)
        this.colorAll = [...colors05, ...colors51, ...colors12]
        this.dataSourceYYYMFW = null
    },
    mounted() {
        this.$nextTick(() => {
            // this.getOutPutTimeList()
        })
    },
    methods: {
        getOutPutTimeList(planID, refresh = false) {
            // planID = '1a7c27cd-e970-4ebd-9911-2737ea652dc6'
            return new Promise((resolve, reject) => {
                this.$iDB.getItem(planID + '_allTime').then(res => {
                    if (refresh && res && res.length > 0) {
                        resolve(res.meshs)
                    } else {
                        var getPoint = waterMeshTool.getPoints(this.basin)
                        var getMesh = waterMeshTool.getMeshs(this.basin)
                        // debugger
                        var getApi = this.$api.Get('/api/CalcPreview/GetMaxDrowning', {
                            ID: planID
                            // ID: this.planCurrSel.id
                        }, 'GetMaxDrowning')
                        Promise.all([getPoint, getMesh, getApi]).then(([resPoint, resMesh, resApi]) => {
                            console.log(resMesh)
                            var colorIndex = 3
                            var meshToSave = []
                            resApi.data.forEach(item => {
                                if (item.h > 2) {
                                    colorIndex = 59
                                } else if (item.h > 1) {
                                    colorIndex = Math.round(item.h * 20 - 20) + 39
                                } else if (item.h > 0.5) {
                                    colorIndex = Math.round((item.h - 0.5) * 0.4 * 100) + 20
                                } else {
                                    colorIndex = Math.round(item.h * 0.4 * 100)
                                }
                                const pointIndex = resMesh.pointIndexs[Number(item.guid)]
                                const lonlats = []
                                pointIndex.forEach(jtem => {
                                    const lonlat = resPoint.lonlats[Number(jtem)]
                                    lonlats.push(...lonlat)
                                })
                                const cartesian3 = Cesium.Cartesian3.fromDegreesArray(lonlats)

                                meshToSave.push({ guid: item.guid, tm: item.tm, h: item.h, cartesian3, colorIndex })
                            })
                            // IDBM.writePlan(planID, { time: 'allTime', meshs: meshToSave })
                            this.$iDB.setItem(planID + '_allTime', meshToSave)
                            resolve(meshToSave)
                        })
                    }
                })
            })
        },
        getMeshYYYMFW(id) {
            // 是否是风险评估
            this.clearYYYMFW()
            // const colorList = ['#CE1F1F', '#F0810B', '#F0C90B', '#00A2FB']
            this.$store.dispatch('mapLoadingText', ['请求数据...'])
            this.getOutPutTimeList(id).then(meshs => {
                if (meshs && meshs.length > 0) {
                    this.drawYYYMFW(meshs, id)
                } else {
                    this.$store.dispatch('mapLoadingText', [])
                    this.getOutPutTimeList(id, true).then(meshs => {
                        if (meshs && meshs.length > 0) {
                            this.drawYYYMFW(meshs, id)
                        } else {
                            console.log('获取数据失败')
                        }
                    })
                }
            })
        },
        drawYYYMFW(meshs, id) {
            this.dataSourceYYYMFW = new Cesium.CustomDataSource('PolylineDataSource')
            meshs.forEach(item => {
                const color = Cesium.Color.fromCssColorString(this.colorAll[item.colorIndex])
                var polyEntity = {
                    eventName: '最大淹没范围',
                    prop: item,
                    polygon: {
                        hierarchy: item.cartesian3,
                        // perPositionHeight: true,
                        material: color.withAlpha(0.5)
                    }
                }
                this.dataSourceYYYMFW.entities.add(polyEntity)
            })
            this.$viewer.dataSources.add(this.dataSourceYYYMFW)
            this.$store.dispatch('mapLoadingText', [])
            this.$eventBus.$emit('openLayer', 'DMX')
            this.$eventBus.$emit('DMXRehearsalID', id)
            this.initEventYYYMFW(id)
        },
        initEventYYYMFW(id) {
            var eventFn = pickedObject => {
                if (
                    pickedObject &&
                    pickedObject.id &&
                    pickedObject.id.eventName
                ) {
                    if (pickedObject.id.eventName === '最大淹没范围') {
                        const dlgParams = this.$store.getters.dlgParams
                        dlgParams.name = 'floodDlog'
                        dlgParams.params = { ID: id, Guid: pickedObject.id.prop.guid }
                        this.$store.dispatch('dlgParams', dlgParams)
                    }
                }
            }
            this.$eventBus.$on('UE_RehearsalYMFW', res => {
                eventFn(res)
            })
            this.$store.getters.eventCesiumOption.leftPickFns.set('最大淹没范围', eventFn)
        },
        clearYYYMFW() {
            if (this.dataSourceYYYMFW) this.$viewer.dataSources.remove(this.dataSourceYYYMFW)
            this.$eventBus.$emit('closeLayer', 'DMX')
            this.$store.getters.eventCesiumOption.leftPickFns.delete('最大淹没范围')
        }
    }
}
